package com.zmn.plat.services.impl.category;

import com.zmn.common.dto.StatusDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.model.entity.category.BaseCategoryAttribute;
import com.zmn.plat.model.entity.category.BaseCategoryAttributeQuery;
import com.zmn.plat.model.vo.category.BaseCategoryAttrVO;
import com.zmn.plat.persistence.interfaces.category.AttributeDAO;
import com.zmn.plat.services.interfaces.category.BaseCategoryAttributeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author duanzuocai
 * @version v1.0
 * @since 2018/10/19 14:21
 */
@Service
public class BaseCategoryAttributeServiceImpl implements BaseCategoryAttributeService {
    @Resource
    AttributeDAO attributeDAO;

    @Override
    public BaseCategoryAttribute findByKey(Integer categoryId) {
        return attributeDAO.findByKey(categoryId);
    }

    @Override
    public List<BaseCategoryAttribute> listAttributeFromChildByCategIdAndAttrName(Integer categId, Integer channelId, String attrName) {
        return attributeDAO.listAttributeFromChildByCategIdAndAttrName(categId, channelId, attrName);
    }

    @Override
    public List<BaseCategoryAttribute> listAttributeByCategIdAndAttrName(Integer categId, Integer channelId, String attrName) {
        return attributeDAO.listAttributeByCategIdAndAttrName(categId, channelId, attrName);
    }

    @Override
    public int countAttributeFromChild(Integer categId, String attrName) {
        return attributeDAO.countAttributeFromChild(categId, attrName);
    }

    @Override
    public int countAttribute(Integer categId, String attrName) {
        return attributeDAO.countAttribute(categId, attrName);
    }

    @Override
    public BaseCategoryAttribute findByName(String name, Integer categId) {
        BaseCategoryAttributeQuery query = new BaseCategoryAttributeQuery();
        query.setCategId(categId);
        query.setName(name);
        List<BaseCategoryAttribute> list = attributeDAO.listByQuery(query);
        if (list != null && list.size() > 0) {
            return null;
        }

        return list.get(0);
    }

    @Override
    public List<BaseCategoryAttrVO> listPageByQuery(BaseCategoryAttributeQuery query) {
        int count = countByQuery(query);
        query.setTotalCount(count);
        return attributeDAO.listPageByQuery(query);
    }

    @Override
    public List<BaseCategoryAttribute> listByCategoryId(Integer categoryId, Integer channelId) {
        if (NumberUtil.isNotNullOrZero(channelId)) {
            return attributeDAO.listByCategoryId(categoryId, 2, channelId);
        } else {
            return attributeDAO.listByCategoryId(categoryId, 1, null);
        }
    }

    @Override
    public List<BaseCategoryAttribute> listByQuery(BaseCategoryAttributeQuery query) {
        return attributeDAO.listByQuery(query);
    }

    @Override
    public Integer countByQuery(BaseCategoryAttributeQuery query) {
        return attributeDAO.countByQuery(query);
    }

    @Override
    public Integer updateByKey(BaseCategoryAttribute attribute) {
        return attributeDAO.updateByKey(attribute);
    }

    @Override
    public int updateStatusBatch(List<Integer> arrayIds, int status, String updater) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(arrayIds);
        dto.setStatus(status);
        dto.setUpdater(updater);

        return attributeDAO.updateStatusBatch(dto);
    }

    @Override
    public int updateSortKey(Integer attrId, Integer sort, String updater) {
        BaseCategoryAttribute attribute = new BaseCategoryAttribute();
        attribute.setAttrId(attrId);
        attribute.setSort(sort);
        attribute.setUpdater(updater);

        return attributeDAO.updateSortKey(attribute);
    }

    @Override
    public Integer insert(BaseCategoryAttribute attribute) {
        return attributeDAO.insert(attribute);
    }

    @Override
    public Integer delete(int id) {
        return attributeDAO.deleteByKey(id);
    }
}
